/*
 * LineScanCamera.c
 *
 *  Created on: Sep 18, 2013
 *      Author: Ricardo
 */



#include "derivative.h" /* include peripheral declarations */
#include "SCR/LineScanCamera.h"
#include "SCR/ADC.h"



/*Funcion que inicializa la camara: configura entradas y salidas de CLK,SI, AO*/
extern volatile unsigned char AnalogCamera[128];
//volatile unsigned char DigitalCamera[128];
void init_camera(void)

{
	SIM_SCGC5 |=     SIM_SCGC5_PORTE_MASK | SIM_SCGC5_PORTD_MASK; //Make sure the clock is enabled for PORTE;
	PORTE_PCR1 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;   //Enable GPIO on on the pin for the CLOCK Signal
	PORTD_PCR7 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;   //Enable GPIO on on the pin for SI signal
	
	PORTD_PCR5 = PORT_PCR_MUX(0); //Make sure AO signal goes to an analog input
	PORTD_PCR6 = PORT_PCR_MUX(0); //Make sure AO signal goes to an analog input
		
	//Make sure the Clock and SI pins are outputs
    GPIOD_PDDR |= (1<<7);
    GPIOE_PDDR |= (1<<1);
            
	
    CAM_CLK_LOW;
	CAM_SI_LOW;
}


void camera(void)		
{	

	unsigned char i=0;
	
	//first conversion for the cam. just for initialize the cmos inside the camera.
	
	CAM_SI_HIGH;
	delay_time(FIRST_CLOCK);		
	CAM_CLK_HIGH;
	delay_time(FIRST_CLOCK);
	CAM_SI_LOW;
	delay_time(FIRST_CLOCK);
	CAM_CLK_LOW;

	
	for(i=0;i<128;i++)
		
	{
		delay_time(FREC_67k);
		CAM_CLK_LOW;
		delay_time(FREC_67k);

		AnalogCamera[i] = camera_value(CAMERA_1);
		CAM_CLK_HIGH;
		
	}
		
		delay_time(FREC_67k);
		CAM_CLK_HIGH;
		delay_time(FREC_67k);
		CAM_CLK_LOW;

	
}


void delay_time(int number){
  int cnt;
  for(cnt=0;cnt<number;cnt++);
}
